package com.sskj.level.ui.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.widget.ViewPagerSlide;
import com.sskj.level.R;
import com.sskj.level.R2;
import com.sskj.level.presenter.TradeListActivityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

//交易单界面
@Route(path = RConfig.LEVEL_TRADE)
public class TradeListActivity extends BaseActivity<TradeListActivityPresenter> {

    ArrayList<Fragment> mFragments;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.tv_all)
    TextView tvAll;
    @BindView(R2.id.tv_history)
    TextView tvHistory;
    @BindView(R2.id.head)
    RelativeLayout head;
    @BindView(R2.id.viewPager)
    ViewPagerSlide viewPager;

    @BindView(R2.id.tv_screen)
    TextView tvScreen;
    @BindView(R2.id.rb_have_done)
    RadioButton rbHaveDone;
    @BindView(R2.id.rb_have_cancel)
    RadioButton rbHaveCancel;
    @BindView(R2.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R2.id.tvCancel)
    TextView tvCancel;
    @BindView(R2.id.tvSure)
    TextView tvSure;
    @BindView(R2.id.v_cancel)
    View vCancel;
    @BindView(R2.id.ll_screen)
    LinearLayout llScreen;

    @Autowired
    public String code;
    /**
     * 0  全部委托
     * 历史 默认不传(本地判断0)   2成交 （已经成交单单子 ），-1撤销’（已经撤销的单子）
     */
    public int tradeStatus = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.level_activity_trade_list;
    }

    @Override
    public TradeListActivityPresenter getPresenter() {
        return new TradeListActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        mFragments = new ArrayList<>();
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.LEVEL_FRAGMENT_TRADE_LIST).withString("fragmentType", "all").navigation());
        mFragments.add((Fragment) ARouter.getInstance().build(RConfig.LEVEL_FRAGMENT_TRADE_LIST).withString("fragmentType", "history").navigation());
        viewPager.setSlide(true);
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                changeTextColor(position);
            }
        });
        ClickUtil.click(tvAll, () -> {
            changeTab(0);
        });
        ClickUtil.click(tvHistory, () -> {
            changeTab(1);
        });
        ClickUtil.click(tvScreen, () -> {
            llScreen.setVisibility(llScreen.isShown() ? View.GONE : View.VISIBLE);
        });
        ClickUtil.click(vCancel, () -> {
            llScreen.setVisibility(View.GONE);
        });
        ClickUtil.click(tvCancel, () -> {
            llScreen.setVisibility(View.GONE);
            radioGroup.clearCheck();
            tradeStatus = 0;
            RxBus.getDefault().send(RxBusCode.LEVEL_TRADE_LIST_STATUS_REFRESH);
        });
        ClickUtil.click(tvSure, () -> {
            llScreen.setVisibility(View.GONE);
            tradeStatus = radioGroup.getCheckedRadioButtonId() == rbHaveDone.getId() ? 2 : -1;
            RxBus.getDefault().send(RxBusCode.LEVEL_TRADE_LIST_STATUS_REFRESH);
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeTab(int position) {
        changeTextColor(position);
        if (viewPager.getCurrentItem() != position) {
            viewPager.setCurrentItem(position, true);
        }
    }

    /**
     * 更改选项卡
     *
     * @param position
     */
    private void changeTextColor(int position) {
        tvScreen.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
        tvAll.setBackground(getResources().getDrawable(position == 0 ? R.drawable.bg_blue_background : R.drawable.bg_blue_line));
        tvHistory.setBackground(getResources().getDrawable(position == 1 ? R.drawable.bg_blue_background : R.drawable.bg_blue_line));
        tvAll.setTextColor(getResources().getColor(position == 0 ? R.color.white : R.color.commonSelectColor));
        tvHistory.setTextColor(getResources().getColor(position == 1 ? R.color.white : R.color.commonSelectColor));
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }

}
